<?php

namespace app\api\controller\yqfff;

/**
 * 一起飞飞飞皮肤相关接口
 */
class Fffskin extends BaseFff
{

    // 获取已拥有皮肤,去重并重新保存
    public function get_skin()
    {
        $type = $this->request->post('type');
        $str = '';
        $have_list = array();
        switch ($type) {
            case 'plane':
                $skin_list = $this->get_json_file('fff_plane_skin_list_define', 'flyflyfly/plane_skin');
                $have_list = array_unique(explode(',', $this->acc->plane_skin_list));
                // 获取新增的飞机皮肤
                foreach ($skin_list as $skin) {
                    if ($skin && $skin->condition && !in_array($skin->id, $have_list)) {
                        if ($this->handle_condition($skin->condition)) {
                            array_push($have_list, $skin->id);
                            if ($skin->bullet !== -1) {
                                $this->acc->bullet_skin_list = $this->acc->bullet_skin_list . ',' . $skin->bullet;
                            }
                        }
                    }
                }
                foreach ($have_list as $skin) {
                    $str = $str . ',' . $skin;
                }
                $this->acc->plane_skin_list = trim(trim($str, ','));
                break;
            case 'bullet':
                $skin_list = $this->get_json_file('fff_bullet_skin_list_define', 'flyflyfly/bullet_skin');
                $have_list = array_unique(explode(',', $this->acc->bullet_skin_list));
                // 获取新增的子弹皮肤
                foreach ($skin_list as $skin) {
                    if ($skin && $skin->condition && !in_array($skin->id, $have_list)) {
                        if ($this->handle_condition($skin->condition)) {
                            array_push($have_list, $skin->id);
                        }
                    }
                }
                foreach ($have_list as $skin) {
                    $str = $str . ',' . $skin;
                }
                $this->acc->bullet_skin_list = trim(trim($str, ','));
                break;
        }
        $this->acc->save();
        $this->result('skin', $have_list);
    }

    // 购买皮肤
    public function buy_skin()
    {
        $type = $this->request->post('type');
        $id = $this->request->post('id');

        if ($type == 'plane') {
            $had_skin_list = explode(',', $this->acc->plane_skin_list);
            $skin_list = $this->get_json_file('fff_plane_skin_list_define', 'flyflyfly/plane_skin');
        } elseif ($type == 'bullet') {
            $had_skin_list = explode(',', $this->acc->bullet_skin_list);
            $skin_list = $this->get_json_file('fff_bullet_skin_list_define', 'flyflyfly/bullet_skin');
        }
        // 判断是否已经拥有
        if (!in_array($id, $had_skin_list)) {
            foreach ($skin_list as $skin) {
                // 指定id皮肤
                if ($skin->id == $id && $skin->price) {
                    // 扣钱
                    if ($this->do_cost($skin->price)) {
                        // 更新皮肤
                        if ($type == 'plane') {
                            $this->acc->plane_skin_list = $this->acc->plane_skin_list . ',' . $id;
                            if ($skin->bullet !== -1) {
                                $this->acc->bullet_skin_list = $this->acc->bullet_skin_list . ',' . $skin->bullet;
                            }
                        }
                        if ($type == 'bullet') {
                            $this->acc->bullet_skin_list = $this->acc->bullet_skin_list . ',' . $id;
                        }
                        // 返回数据
                        if ($this->acc->save() !== false) {
                            $data['coin'] = $this->acc->coin;
                            $data['diamond'] = $this->acc->diamond;
                            $this->result('ok', $data);
                        }
                    }
                }
            }
        }

        $this->result('fail');
    }

    // 扣费操作
    public function do_cost($price_str)
    {
        $arr = explode('_', $price_str);
        $type = intval($arr[0]);
        $cost = intval($arr[1]);
        if ($type == 1) {
            if ($cost <= $this->acc->coin) {
                $this->acc->coin -= $cost;
                return true;
            }
        } elseif ($type == 2) {
            if ($cost <= $this->acc->diamond) {
                $this->acc->diamond -= $cost;
                return true;
            }
        }
        return false;
    }

    // 切换皮肤
    public function change_skin()
    {
        $type = $this->request->post('type');
        $skin_id = $this->request->post('skin_id') ?: 1;

        if ($type == 'bullet') {
            $this->acc->bullet_skin = $skin_id;
        }
        if ($type == 'plane') {
            $this->acc->plane_skin = $skin_id;
            $list = $this->get_json_file('fff_plane_skin_list_define', 'flyflyfly/plane_skin', true);
            foreach ($list as $skin) {
                if ($skin->id == $skin_id) {
                    if ($skin->bullet !== -1) {
                        $this->acc->bullet_skin = $skin->bullet;
                    }
                }
            }
        }
        $this->acc->save();
        $data['plane_skin'] = $this->acc->plane_skin;
        $data['bullet_skin'] = $this->acc->bullet_skin;
        $this->result('ok', $data);
    }

    // 用户数据结算
    public function get_balance()
    {
        $resurrect_num = $this->request->post('resurrect_num') ?: 0; // 本局复活次数
        $block_num = $this->request->post('block_num') ?: 0; // 本局打砖块数量
        $score = $this->request->post('score') ?: 0; // 本局得分
        $live_second = $this->request->post('live_second') ?: 0; // 本局存活时间
        $double_coin = $this->request->post('double_coin') ?: 1; // 是否双倍

        // 更新数据
        $this->acc->game_num += 1;
        $this->acc->resurrect_num += $resurrect_num;
        $this->acc->block_num += $block_num;
        if ($this->acc->max_score < $score) {
            $this->acc->max_score = $score;
        }
        if ($this->acc->live_second < $live_second) {
            $this->acc->live_second = $live_second;
        }
        $this->acc->coin += floor($score / 200) * $double_coin;
        $this->acc->all_score += $score;

        $this->acc->save();

        $data['coin'] = $this->acc->coin;
        $data['diamond'] = $this->acc->diamond;
        $this->result('ok', $data);
    }

}
